package com.hao.tool.web.module.navigation.controller;

import com.hao.tool.common.constant.Commons;
import com.hao.tool.common.entity.LayResponse;
import com.hao.tool.web.component.websocket.NoticeConsumer;
import com.hao.tool.common.enums.ShortEnum;
import com.hao.tool.web.module.base.entity.ShortInfo;
import com.hao.tool.web.module.base.service.ShortInfoService;
import com.hao.tool.web.module.navigation.entity.NoticeInfo;
import com.hao.tool.web.module.navigation.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 22:17
 */
@RestController
@RequestMapping("/rest/notice")
public class NoticeRestController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private ShortInfoService shortInfoService;
    @Autowired
    private NoticeConsumer noticeConsumer;

    @PostMapping("/add")
    public LayResponse add(NoticeInfo notice) {
        noticeService.saveNotice(notice);
        LayResponse layResponse = new LayResponse();
        layResponse.setCode(LayResponse.SUCCESS);
        layResponse.setMsg("新增成功");
        return layResponse;
    }

    @PostMapping("/edit")
    public LayResponse edit(NoticeInfo notice) {
        noticeService.updateById(notice);
        LayResponse layResponse = new LayResponse();
        layResponse.setCode(LayResponse.SUCCESS);
        layResponse.setMsg("新增成功");
        return layResponse;
    }

    @PostMapping("/push")
    public LayResponse push(String message) {
        noticeConsumer.pushLogger(message);
        LayResponse layResponse = new LayResponse();
        layResponse.setCode(LayResponse.SUCCESS);
        layResponse.setMsg("新增成功");
        return layResponse;
    }

    @PostMapping("/addShort")
    public LayResponse shortEdit(String longId, String shortId,
                                 HttpServletRequest request) {
        String userName = (String) request.getAttribute(Commons.SUB_KEY);
        ShortInfo shortInfo = new ShortInfo(shortId, longId);
        shortInfo.setShortType(ShortEnum.NOTICE.getCode());
        shortInfo.setCreateUser(userName);
        shortInfoService.saveOrUpdate(shortInfo);
        return LayResponse.builder().code(200).msg("").data(shortInfo).build();
    }

}
